`
https://leetcode.cn/problems/maximum-sum-of-almost-unique-subarray/
`

/**
 * @param {number[]} nums
 * @param {number} m
 * @param {number} k
 * @return {number}
 */
var maxSum = function (nums, m, k) {
  const n = nums.length
  const counter = new Map()
  let sum = 0
  let maxSum = 0
  let left = 0, right = 0

  // 维护一个长度为 k 的滑动窗口
  while (right < n) {
    const n1 = nums[right++]
    sum += n1
    counter.set(n1, (counter.get(n1) || 0) + 1)

    if (right - left >= k) {
      // 窗口长度为 k 时更新答案
      if (counter.size >= m) {
        maxSum = Math.max(maxSum, sum)
      }
      const n2 = nums[left++]
      sum -= n2
      counter.set(n2, (counter.get(n2) || 0) - 1)
      if (counter.get(n2) <= 0) counter.delete(n2)
    }
  }

  return maxSum
};